package com.woniu.springbootshiro.controller;


import com.woniu.springbootshiro.pojo.LampInfo;
import com.woniu.springbootshiro.pojo.LampStrategy;
import com.woniu.springbootshiro.service.LampInfoService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

@MultipartConfig
@Controller
public class LampInfoController {
  @Autowired
  private LampInfoService lampInfoService;

  @RequestMapping("/querySetLight")
  @ResponseBody
  public List<LampInfo> queryAllSetLight() {
    System.out.println(lampInfoService.queryAllLamp());
    return lampInfoService.queryAllLamp();
  }


  @RequestMapping("/deleteLamp")
  @ResponseBody
  public String deleteOne(Integer id) {
    int status = lampInfoService.deleteOne(id);
    String status1 = String.valueOf(status);
    return status1;
  }


  @RequestMapping("/deleteAllLamp")
  @ResponseBody
  public String deleteAll(String ids) {
    String[] idArr = ids.split(",");
    int temp = 0;
    for (int i = 0; i < idArr.length; i++) {
      temp = Integer.parseInt(idArr[i]);
      lampInfoService.deleteOne(temp);
    }
    return "1";
  }

  @PostMapping("/addLamp")
  @ResponseBody
  public String upload(@RequestParam("file") MultipartFile file, LampInfo lampInfo) {

    String serverPath = "C:/Users/EDZ/AppData/Local/Temp/tomcat-docbase.3566214387297353586.8081/img/";
    String imgStr = null;
    if (file != null && file.getSize() > 0) {
      imgStr = UUID.randomUUID().toString().replace("-", "") + file.getOriginalFilename();
      System.out.println(serverPath + imgStr);
      try {
        file.transferTo(new File(serverPath + imgStr));
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    lampInfo.setLampImg( "tomcat-docbase.3566214387297353586.8081/img/" + imgStr);
    String xy = "(" + lampInfo.getX() + "," + lampInfo.getY() + ")";
    lampInfo.setXy(xy);
    System.out.println("insert:" + lampInfoService.insertOneLamp(lampInfo));
    return "1";
  }

  @RequestMapping("/queryLampById")
  @ResponseBody
  public LampInfo queryLampById(HttpServletRequest req) {
    System.out.println("entering querylampbyid controller");
    String id = req.getParameter("id");
    System.out.println("id:" + id);
    Integer ids = Integer.parseInt(id);
    LampInfo lampInfo = lampInfoService.queryLampById(ids);
    System.out.println("lampInfo:"+lampInfo);
    return lampInfo;
  }

  @RequestMapping("/updateLamp")
  public int updateLamp(LampInfo lampInfo, HttpServletResponse resp) {
 return lampInfoService.updateLampById(lampInfo);
  }



}
